package com.frx.leetcode.one;

import java.util.Stack;

/**
 * @Author: xrf96
 * @Description: 用栈实现队列
 * @Date: 2020/1/9 12:48
 * @Version: 1.0
 */
public class MyQueue {

    private Stack<Integer> input;
    private Stack<Integer> output;

    public static void main(String[] args){
        MyQueue queue = new MyQueue();
        queue.push(1);
        queue.push(2);
        System.out.println(queue.peek());  // 返回 1
        System.out.println(queue.pop());   // 返回 1
        System.out.println(queue.empty()); // 返回 false

    }

    public MyQueue(){
        input = new Stack<>();
        output = new Stack<>();
    }

    public void push(int x){
        input.push(x);
    }

    public int pop(){
        if(empty()){
            throw new RuntimeException("Queue is empyt.");
        } else{
            if(output.empty()){
                while(!input.empty()){
                    input.push(output.pop());
                }
            }
        }
        int e = output.pop();
        return e;

/*        if (output.empty()){
            while (!input.empty()){
                output.push(input.pop());
            }
        }
        int e = output.pop();
        return e;*/
    }

    public int peek(){
        if (output.empty()){
            while (!input.empty()){
                output.push(input.pop());
            }
        }
        int e = output.peek();
        return e;
    }

    public boolean empty(){
        return input.empty() && output.empty();
    }
}
